package cn.magic.controller;

import cn.magic.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

        @HystrixCommand(fallbackMethod = "getByUserIdFallbackMethod") //方法熔断
    @GetMapping("/order/{id}")
    public User getByUserId(@PathVariable Long id){
        String url = "http://user-server/user/"+id;
        return restTemplate.getForObject(url,User.class);
    }

    /**
    * @Title: getByUserIdFallbackMethod
    * @Description: 降级方法
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/28 19:03
    * @Parameters: [id]
    * @Return cn.magic.domain.User
     *
     *     1.方法名需要和fallbackMethod指定的方法名要一致
     *     2.返回的类型要和原来的一致
     *     3.请求的参数要一致
     *     把原来的方法复制过来  方法体自己重新根据实际的需求  写一下
     *     把fallbackMethod指定的方法名复制过来
    */
    public User getByUserIdFallbackMethod(@PathVariable Long id){
        return User.builder().id(-1L).into("降级了").build();
    }



}
